﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace CSV_Tool
{
    //将已经读取的CSV数据转换为需要用的数据
    public static class CSVConverter
    {

        //转换单列CSV数据
        public static T ConvertCSVToUnity<T>(string[] parameter) where T : ICSVData, new()
        {
            T value = new T();
            value.CreateCSVData(parameter);
            return value;
        }

        //转换整个表的CSV数据____二维数组版本
        public static T[] ConvertAllCSVToUnity<T>(string[,] parameter) where T : ICSVData, new()
        {
            List<T> list = new List<T>();
            for (int count = 0; count < parameter.GetLength(0); count++)
            {
                string[] line = new string[parameter.GetLength(1)];
                for (int count2 = 0; count2 < parameter.GetLength(1); count2++)
                    line[count2] = parameter[count, count2];

                list.Add(ConvertCSVToUnity<T>(line));
            }
            return list.ToArray();
        }

        public static T[] ConvertAllCSVToUnity<T>(string[][] parameter) where T:ICSVData,new()
        {
            List<T> list = new List<T>();

            for(int count = 0;count < parameter.Length; count++)
            {
                var selectLine = parameter[count];
                var convertedItem = ConvertCSVToUnity<T>(selectLine);

                list.Add(convertedItem);
            }

            return list.ToArray();
        }

        //转换整个表的CSV数据，并按某个字段的值进行分类____二维数组
        public static T[][] ConvertAllCSVToUnity<T>(string[,] parameter, int index) where T : ICSVData, new()
        {
            Dictionary<string, List<T>> returnList = new Dictionary<string, List<T>>();

            for (int count = 0; count < parameter.GetLength(0); count++)
            {
                var key = parameter[count, index];

                if (!returnList.ContainsKey(key))
                    returnList.Add(key, new List<T>());

                returnList[key].Add(ConvertCSVToUnity<T>(CatchOneLine(parameter, count)));
            }

            List<T>[] valueArray = new List<T>[returnList.Count];
            T[][] returnArrays = new T[returnList.Count][];
            returnList.Values.CopyTo(valueArray, 0);

            for (int count = 0; count < returnArrays.Length; count++)
            {
                returnArrays[count] = valueArray[count].ToArray();
            }

            return returnArrays;
        }

        //从整个表中获取指定列____Strings of Strings版本
        public static string[] CatchOneLine(string[][] parameter, int index)
        {
            return parameter[index];
        }

        //从整个表中获取指定列____二维数组版本
        public static string[] CatchOneLine(string[,] parameter, int index)
        {
            string[] specifyLine = new string[parameter.GetLength(1)];
            for (int count = 0; count < parameter.GetLength(1); count++)
            {
                specifyLine[count] = parameter[index, count];
            }
            return specifyLine;
        }
    }
}
